package com.tzraeq.quercus.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tzraeq.quercus.http.PreURIDecodeHttpServletRequestWrapper;

public class PreURIDecodeFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;  
        HttpServletResponse response = (HttpServletResponse) servletResponse;  
  
        // 获得请求的方式 (1.post or 2.get), 根据不同请求方式进行不同处理  
        String method = request.getMethod();  
        // 1. 以 post 方式提交的请求 , 直接设置编码为 UTF-8  
        if (method.equalsIgnoreCase("post") && request.getContentType().startsWith("application/x-www-form-urlencoded")) {  
        	
        	/*if (contentType != null
                    && contentType.startsWith("application/x-www-form-urlencoded"))
                  StringUtility.parseStr(env, bb, postArray, true, encoding, true);*/
        	request = new PreURIDecodeHttpServletRequestWrapper(request);
        }  
        
        chain.doFilter(request, response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
